So once you have labored over making a leaflet map and connecting all the observe() calls to get layers to toggle on and off, you deserve to know the shortcut.

Enter mapview.

knitr::include_graphics("images/headexplosionemoji.jpg")

Mapview basically uses the leaflet package and builds upon it by functionalizing a bunch of the parts of the map you just labored over e.g. toggling layers on and off, basemaps, popups (we didn’t dig in but with a few more strategically placed paste() statements, you could have a pretty intricate popup going on there). This is not to say leaflet is dead to me, on the contrary, I use it frequently. It just depends on how much control you want over your map. If your want mild to moderate control/customization, use mapview. If you want to get dirty in some code, use leaflet. You want to be a mapping ninja? Use both in the same map (see head explosion emoji for emphasis).

Before we really dig in to mapview, let’s get spatial data into R with a different tool: sf.

sf is sp 2.0. What is sp? sp was the package you used to get spatial data into R (less rasters because you use the raster package for that). sp was great. sp is great, but it’s complicated and slow. Roger Bivand is all you need to know about sp. Look him up, tons of resources on spatial data processing in R, he’s a legend. BUTTTTTT, in ~ 2014ish sf was published and it was the new kid on the block. It took a few years to get people on board- because once you invested in sp over Arc products/GRASS/python, man, you were invested- but the sun is setting on sp. I still HIGHLY encourage you learn sp for reference, but sf is where you should be spending your coding efforts. sf takes advantage of the tidyverse and uses ‘sticky’ dataframes/tibbles such that spatial data is nested with table data in a neatly organized list column. In comparison, sp used S4 objects where data and spatial information were organized into ‘slots’ and (unless you were pretty well versed in sp) you could easily lose data or not complete the spatial analysis you thought you were doing. sf is great. End of story.

For more information on sf, see Robin Lovelace’s great book (online fo free): https://geocompr.robinlovelace.net/ Fabulous resource and my favorite on the topic. Of course there are many more with a simple google search.

Let’s bring in that same data we were using for 3_leafletMapApp. We are also going to use the sf library to quickly change the flat file into a spatial object so it can be plotted easily.

probData <- readRDS('C:/HardDriveBackup/R/GitHub/IntroToShiny/3_leafletMapApp/data/probData.RDS') %>% # you probably need to change absolute file location
  st_as_sf(coords = c("LongitudeDD", "LatitudeDD"),  # make spatial layer using these columns
           remove = F, # don't remove these lat/lon cols from df
           crs = 4326) # add coordinate reference system, needs to be geographic for now bc entering lat/lng, 

Quick have a look at what turning the probData.RDS into an sf object looks like. Hint: go to far right of table and see geometry listcolumn.

View(probData)
str(probData)
## Classes 'sf', 'tbl_df', 'tbl' and 'data.frame':  646 obs. of  20 variables:
##  $ StationID  : chr  "1AAUA017.60" "1ABED009.37" "1ABUL009.61" "1ACAA000.83" ...
##  $ LongitudeDD: num  -77.5 -77.5 -77.4 -77.6 -77.3 ...
##  $ LatitudeDD : num  38.5 38.6 38.8 38.8 39 ...
##  $ Year       : num  2005 2013 2005 2003 2001 ...
##  $ Basin      : chr  "Potomac-Shenandoah" "Potomac-Shenandoah" "Potomac-Shenandoah" "Potomac-Shenandoah" ...
##  $ VSCIVCPMI  : num  61.1 69.1 42 39.2 43.6 ...
##  $ DO         : num  8.97 8.63 9.46 11.32 9.55 ...
##  $ pH         : num  7.16 7.17 7.63 7.4 7.3 ...
##  $ SpCond     : num  109.7 48.5 635 150.5 157 ...
##  $ TP         : num  0.04 0.04 0.04 0.02 0.09 0.05 0.055 0.06 0.04 0.02 ...
##  $ TN         : num  0.38 0.385 7.18 0.63 4.95 0.805 0.87 0.83 0.74 0.21 ...
##  $ TotHab     : num  150 178 134 162 156 ...
##  $ LRBS       : num  -0.0864 -0.3103 NA NA NA ...
##  $ MetalCCU   : num  1.236 1.169 0.432 0.721 NA ...
##  $ TDS        : num  59 33 319 85 115 116 10 98 104 4 ...
##  $ Na         : num  3.55 3.84 37.7 NA NA 8.33 7.64 6.1 6.39 7.95 ...
##  $ K          : num  0.82 0.66 7.44 NA NA 1.9 2.58 2.65 1.59 1.34 ...
##  $ Cl         : num  3.94 1.99 53.2 NA NA 18.5 15.6 8.03 10.4 9.47 ...
##  $ Sf         : num  2.85 1.92 64.3 NA NA 16.8 11 12.6 11.4 12.8 ...
##  $ geometry   :sfc_POINT of length 646; first list element:  'XY' num  -77.5 38.5
##  - attr(*, "sf_column")= chr "geometry"
##  - attr(*, "agr")= Factor w/ 3 levels "constant","aggregate",..: NA NA NA NA NA NA NA NA NA NA ...
##   ..- attr(*, "names")= chr  "StationID" "LongitudeDD" "LatitudeDD" "Year" ...

mapview. BOOM.

mapview(probData) 

So the default map is pretty awesome, but also pretty basic. You see you have a legend (top right corner), layer control (top left under zoom), 5 preset basemaps!, built in popupTable (click any point to see), built in hover popup (hover mouse on any point to see), quick toggling of your data on and off, and go to bottom right corner for a prebuilt home button (takes you back to dataset if ever you get lost or zoom too far in/out).

Mapview is pretty awesome.

You can also customize it pretty easily. This is just one example.

# choose only a few basemaps, turn off legend, set a color palette, choose default NA color
mapviewOptions(basemaps = c( "OpenStreetMap",'Esri.WorldImagery'),
               vector.palette = colorRampPalette(brewer.pal(3, "Set1")),
               na.color = "magenta",
               legend=FALSE)

mapview(probData, 
        label = probData$StationID, # set your hover field
        color = 'yellow', # feature outer color
        layer.name = 'ProbMon Sites 2001 - 2014', # default layer name
        popup= popupTable(probData, zcol = c('StationID', 'Year', 'Basin', 'VSCIVCPMI')) # limit popup table to these columns
        ) 

The second map makes so much more sense.

You can also add multiple layers to a single map.

streams <- st_read('C:/HardDriveBackup/R/GitHub/IntroToShiny/3_leafletMapApp/data/Streams_wgs84.shp') # you will need to change the file location to your absolute file location since we are going up a directory
## Reading layer `Streams_wgs84' from data source `C:\HardDriveBackup\R\GitHub\IntroToShiny\3_leafletMapApp\data\Streams_wgs84.shp' using driver `ESRI Shapefile'
## Simple feature collection with 825 features and 11 fields
## geometry type:  LINESTRING
## dimension:      XY
## bbox:           xmin: -83.61143 ymin: 36.31813 xmax: -75.24021 ymax: 39.44017
## epsg (SRID):    4326
## proj4string:    +proj=longlat +datum=WGS84 +no_defs
ecoL3 <- st_read('C:/HardDriveBackup/R/GitHub/IntroToShiny/3_leafletMapApp/data/vaECOREGIONlevel3__proj84.shp')# you will need to change the file location to your absolute file location since we are going up a directory
## Reading layer `vaECOREGIONlevel3__proj84' from data source `C:\HardDriveBackup\R\GitHub\IntroToShiny\3_leafletMapApp\data\vaECOREGIONlevel3__proj84.shp' using driver `ESRI Shapefile'
## Simple feature collection with 7 features and 1 field
## geometry type:  MULTIPOLYGON
## dimension:      XY
## bbox:           xmin: -83.67541 ymin: 36.54074 xmax: -75.24227 ymax: 39.46602
## epsg (SRID):    4326
## proj4string:    +proj=longlat +datum=WGS84 +no_defs
mapview(probData, 
        label = probData$StationID, # set your hover field
        color = 'yellow', # feature outer color
        layer.name = 'ProbMon Sites 2001 - 2014', # default layer name
        popup= popupTable(probData, zcol = c('StationID', 'Year', 'Basin', 
                                             'VSCIVCPMI')) # limit popup table to these columns
        ) +
  mapview(streams, 
          color = 'blue',
          layer.name = 'Major Rivers',
          label = streams$NAME) +
  mapview(ecoL3, 
          color = 'black',
          layer.name = 'Level III Ecoregions',
          label = ecoL3$US_L3NAME) 

Moral of the story- sf is great, mapview is great (especially for quick data viewing), leaflet is great. Your preference for a given project or part of project just depends on your needs. Become familiar with all packages overviewed in this short script and you will be sure to have a lot of options right at your fingertips.